﻿/************************************************************************

 This file is part of EscapeFromPrisonPlanet.

 EscapeFromPrisonPlanet is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 EscapeFromPrisonPlanet is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Foobar.  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2012 Scott Bevin. all rights reserved

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LudumDare25Project.GameScreens
{
    internal class GameOverScreen : GameScreen
    {
        private SpriteFont font;
        private SpriteFont hintFont;
        private float score;

        private WhiteFadeScreen whiteFadeOut = null;
        private GameScreen gamescreenToTrigger = null;

        private Menu menu;

        private string[] hints =
        {
            "Use the W & S keys to move up and down",
            "Used the A & D keys to speed up and slow down",
            "Left click to shoot your guns",
            "Right click to shoot a missile",
            "Try not to fly into asteroids",
            "Try not to fly into other spacecraft",
            "Try not to fly into boobytrapped cargo",
            "Try to save your missiles for asteroid walls",
            "Don't fly into the top or bottom asteroid belts",
            "Destroy enemy spacecraft so that you can steal the cargo left behind",
            "Don't worry too much if you start seeing goats",
            "Don't forget to vote",
            "Don't forget to vote",
        };

        private enum HintState
        {
            FadingIn,
            WaitingIn,
            FadingOut,
            WaitingOut,
        }

        private HintState hintState = HintState.FadingIn;
        private float hintTimer = 0.0f;
        private string hintStr = "";
        private float hintAlpha;

        private Random rnd = new Random();

        public GameOverScreen(float score)
            : base(true)
        {
            this.score = score;
        }

        public override void OnAdded()
        {
            font = ContentManager.Load<SpriteFont>("Fonts/Linds48");
            hintFont = ContentManager.Load<SpriteFont>("Fonts/Linds12");

            menu = new Menu(ContentManager.Load<SpriteFont>("Fonts/Linds24"), ContentManager);

            menu.AddEntry("Retry", this.Rety);
            menu.AddEntry("Quit To Main Menu", this.MainMenu);
            menu.AddEntry("Quit Game", this.QuitGame);

            hintStr = hints[rnd.Next(hints.Length)];

            base.OnAdded();
        }

        public override void Update(float dt, InputManager input)
        {
            menu.Update(dt, input, Game.GraphicsDevice);

            /*if (whiteFadeOut == null)
            {
                if (input.MouseButtonStates[(int)InputManager.MouseButtons.LeftButton].Released ||
                    input.MouseButtonStates[(int)InputManager.MouseButtons.RightButton].Released)
                {
                    whiteFadeOut = new WhiteFadeScreen();
                    gamescreenToTrigger = new MainMenuScreen();

                    GameScreenManager.AddGameScreen(whiteFadeOut, null, LudumDare25Project.GameScreenManager.AddGameScreenData.PositioningOptions.OnTop);
                }
            }*/

            if (whiteFadeOut != null && whiteFadeOut.State == WhiteFadeScreen.States.FadedIn)
            {
                whiteFadeOut.FadeOut();
                GameScreenManager.AddGameScreen(gamescreenToTrigger, whiteFadeOut, LudumDare25Project.GameScreenManager.AddGameScreenData.PositioningOptions.BelowRef);
                this.KillScreen = true;
            }

            ProcessHint(dt);

            base.Update(dt, input);
        }

        private void ProcessHint(float dt)
        {
            if (hintState == HintState.FadingIn)
            {
                hintTimer += dt;
                hintAlpha = hintTimer / 1.0f;

                if (hintTimer >= 1.0f)
                {
                    hintState = HintState.WaitingIn;
                    hintTimer = 0.0f;
                }
            }
            else if (hintState == HintState.WaitingIn)
            {
                hintTimer += dt;
                hintAlpha = 1.0f;

                if (hintTimer >= 5.0f)
                {
                    hintState = HintState.FadingOut;
                    hintTimer = 0.0f;
                }
            }
            else if (hintState == HintState.FadingOut)
            {
                hintTimer += dt;
                hintAlpha = 1.0f - hintTimer / 1.0f;

                if (hintTimer >= 1.0f)
                {
                    hintState = HintState.WaitingOut;
                    hintTimer = 0.0f;
                }
            }
            else if (hintState == HintState.WaitingOut)
            {
                hintTimer += dt;
                hintAlpha = 0.0f;

                if (hintTimer >= 0.5f)
                {
                    hintStr = hints[rnd.Next(hints.Length)];
                    hintState = HintState.FadingIn;
                    hintTimer = 0.0f;
                }
            }
        }

        public override void Render(float dt, GraphicsDevice gd, SpriteBatch sprites)
        {
            sprites.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);

            string text = String.Format("Game Over! You got {0,5:f} lightyears away!", score);

            sprites.DrawString(font, text, new Vector2(500.0f, 200.0f), Color.Black, 0.0f,
                font.MeasureString(text) * 0.5f, 1.0f, SpriteEffects.None, 0.0f);

            text = "Helpful Hint: " + hintStr + ".";

            sprites.DrawString(hintFont, text, new Vector2(500.0f, 500.0f), Color.Black * hintAlpha, 0.0f,
                hintFont.MeasureString(text) * 0.5f, 1.0f, SpriteEffects.None, 0.0f);

            sprites.End();

            menu.Render(gd, sprites);

            base.Render(dt, gd, sprites);
        }

        private void Rety(Menu.MenuEntry entry)
        {
            whiteFadeOut = new WhiteFadeScreen();
            gamescreenToTrigger = new GameplayScreen();

            GameScreenManager.AddGameScreen(whiteFadeOut, null, LudumDare25Project.GameScreenManager.AddGameScreenData.PositioningOptions.OnTop);
        }

        private void MainMenu(Menu.MenuEntry entry)
        {
            whiteFadeOut = new WhiteFadeScreen();
            gamescreenToTrigger = new MainMenuScreen();

            GameScreenManager.AddGameScreen(whiteFadeOut, null, LudumDare25Project.GameScreenManager.AddGameScreenData.PositioningOptions.OnTop);
        }

        private void QuitGame(Menu.MenuEntry entry)
        {
            this.Game.Exit();
        }
    }
}